#ifndef _CITY_MAP_H_
#define _CITY_MAP_H_

#include <string>
#include <vector>
#include "math/vec2.h"
#include "input/keyboard.h"
#include "input/mouse.h"
#include "math/spline2d.h"
#include "num/graph.h"
#include "math/counter.h"

namespace LuaPlus
{
	class LuaState;	
}

namespace	background
{
class  BGScene;
};


namespace script
{
	class WriteLua;
}
namespace city
{
struct						CityData
{
	int						ID;
	std::string				Name;
	std::string				Description;
	std::string				Map;
	std::string				MouseOff;
	std::string				MouseOver;
	std::string				Script;
	std::string				Condition;
};
class CityNode;
class CityPath;
class CityEditor;
class CityActor;
class CityMap
{
public:
	CityMap();
	virtual ~CityMap();
	virtual void					Render();
	virtual void					RenderDebug();
	virtual void					Tick(float dt);
	virtual bool					LoadData(const std::string& File);
	virtual bool					Load(const std::string& Name);
	virtual bool					Save(const std::string& Name);
	const math::Vec2&				GetOffset() const { return m_Offset; };
	void							SetOffset(const math::Vec2& V) { m_AbsOffset = V; };
	math::Vec2						GetMax();

	virtual void					InitUI() {};
	virtual void					ShutdownUI(){};
	virtual void					RenderUI(){};
	void							MouseScroll(float dt);


	// paths
	bool						AddPath(const math::SplineCache2D& Cache, int From, int To, int Type = 0);
	CityPath*					GetPath(const math::Vec2& Pos);
	bool						DeletePath(const math::Vec2& Pos);

	// nodes
	CityNode*					AddNode(const math::Vec2& Pos, const std::string& Type, int Quality, int ID = -1);
	bool						DeleteNode(const math::Vec2& Pos);
	CityNode*					GetNode(const math::Vec2& Pos);
	CityNode*					GetNode(int ID);
	CityNode*					GetNode(const std::string& Name);
	CityPath*					GetConnection(CityNode*	pA, CityNode*	pB, bool & Reverse);

	void						AddActor(CityActor*	 pActor)	{ m_Actors.push_back(pActor); };
	void						DeleteActor(CityActor*	 pActor);

	virtual	void				MouseButtonDown(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	virtual	void				MouseButtonPressed(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	virtual void				MouseMove(const math::Vec2& From, const math::Vec2& To);
	virtual void				MouseWheelMove(int WheelInfo, const math::Vec2& Pos);
	virtual void				KeyPressed(input::Keyboard::KEY* pKey);
	virtual void				KeyDown(input::Keyboard::KEY* pKey);

	bool						IsEditing() const { return false; }	
	background::BGScene*		GetBackground() { return m_pBackground; };
	CityEditor*					GetEditor() { return m_pEditor; };

	CityData*					GetCityData(int ID);
	static	void				Register(LuaPlus::LuaState* _pScript);
	const math::Vec2&			GetFocus() const { return m_FocusPoint; };


	// virtual operations
	virtual void				LoadCustom(LuaPlus::LuaState& State) {};
	virtual void				SaveCustom(script::WriteLua& WL) {};
	virtual void				GameLoaded() {} ;
	virtual void				NodeClicked(CityNode* pNode);
	num::Graph*					GetGraph() { return &m_Graph; };

	void						SetTarget(CityActor*	pTarget) { m_pTarget = pTarget; };

	
protected:
	void						Reset();
	background::BGScene*			m_pBackground;
	math::Vec2						m_Offset, m_AbsOffset;
	math::Vec2						m_FocusPoint;
	CityEditor*						m_pEditor;
	std::vector<CityNode*>			m_Nodes;
	std::vector<CityPath*>			m_Paths;
	std::vector<CityData*>			m_CityData;
	std::vector<CityActor*>			m_Actors;
	num::Graph						m_Graph;
	void							RebuildGraph();

	math::Counter					m_Turn;
	int								m_CurrentTurn;

	CityActor*						m_pTarget;
	bool							m_CanScroll;
};

};

#endif // _CITY_MAP_H_